/**
 * 
 */
define([ 'knockout', 'platform.common', 'titleview', 'bs.table', 'bs.tree',
		'bs.validator','fileinput','fileinput-zh','gridview' ], function(ko, platform, titleview) {
	return {
		$form : $('detailForm'),
		$modal : $('#attachemntModal'),
		attachmentId : ko.observable(),
		attachmentName : ko.observable(),
		isSelectFiles : ko.observable(false),
		isUploaded : ko.observable(false),
		initForm : function() {
			var that = this;
			that.clear();
			that.attachmentId(platform.uuid());
			$('#files').fileinput(
			{
				showUpload : false,
				uploadAsync : true,
				autoReplace: true,
				overwriteInitial: true,
			    showUploadedThumbs: false,
			    showCaption:true,
			    showPreview:true,
				uploadAsync : true,
				uploadUrl : platform.mapUrl('~/rest/attachment/uploadFile'),
				uploadExtraData : {
					fileName:document.getElementById('fileName').value,
					fileId : that.attachmentId()
				},
				language : 'zh',
				allowedPreviewTypes : [ 'image', 'html', 'text', 'video', 'audio', 'flash', 'object' ],
				allowedFileExtensions : [ 'jpg', 'gif', 'png', 'txt', 'xls', 'xlxs', 'doc', 'docx', 'pdf', 'rar' ],
				maxFileCount : 1,
				minFileCount : 1,
				fileActionSettings:{
					showUpload:false,
					showRemove:false
				}
			}).on('fileloaded',function(event, file, previewId, index, reader) {
				that.attachmentName(file.name);
				document.getElementById('fileName').value = file.name;
				that.isSelectFiles(true);
			}).on('filecleared', function(event) {
				that.isSelectFiles(false);
				that.isUploaded(false);
			}).on('filepreupload', function(event, data, previewId, index) {
			}).on('fileuploaded',function(event, file, previewId, index, reader) {
				that.isUploaded(true);
				var barview = require('barview');
				var gridview = require('gridview');
				if (file.response
						&& file.response.success) {
					barview.fnCloseDlg();
					gridview.refresh();
				}
			}).fileinput('clear');
		},
		resetForm : function() {
			if (this.$form.data('bootstrapValidator') != null
					|| $form.data('bootstrapValidator') != undefined) {
				$form.data('bootstrapValidator').resetForm();
			}
		},
		validateForm : function() {
			this.$form.bootstrapValidator({
				message : 'This value is not valid',
				feedbackIcons : {
					valid : 'glyphicon glyphicon-ok',
					invalid : 'glyphicon glyphicon-remove',
					validating : 'glyphicon glyphicon-refresh'
				},
				excluded : [ ':disabled' ],
				fields : {
					attachmentName : {
						validators : {
							notEmpty : {
								message : '附件名称为必填字段,不可为空'
							}
						}
					}
				}
			});
		},
		clear : function() {
			this.attachmentId('');
			this.attachmentName('');
		},
		showNewForm : function() {
			this.clear();
			this.initForm();
			titleview.title('附件上传');
			this.validateForm();
			this.$modal.modal('show');
		}
	};
});